home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2001 / MacHack 2001.toast / pc / The Hacks / PalmDock / Src / Starter.cpp next >
Encoding:
C/C++ Source or Header  |  2001-06-23  |  14.3 KB  |  628 lines

  1. /******************************************************************************
  2.  *
  3.  * PalmDock -- an OS X Dock for your Palm
  4.  *
  5.  *  Rob Tsuk
  6.  *  Nevin “:-)” Liber
  7.  *  Sanford Selznick
  8.  *
  9.  *****************************************************************************/
  10.  
  11. #include <PalmOS.h>
  12. #include "StarterRsc.h"
  13.  
  14. #include <vector>
  15. #include <string>
  16.  
  17. using std::vector;
  18. using std::string;
  19.  
  20. vector<MemHandle> gSmallIcons;
  21. vector<MemHandle> gLargeIcons;
  22. vector<Coord> gIconLefts;
  23. vector<UInt32> gCreators;
  24. vector<string> gNames;
  25.  
  26. static Int32 gTappedIcon = -1;
  27. Boolean gDragging = false;
  28.  
  29. static void LoadIcons();
  30. static void DrawIcons();
  31.  
  32. /***********************************************************************
  33.  *
  34.  *   Entry Points
  35.  *
  36.  ***********************************************************************/
  37.  
  38.  
  39. /***********************************************************************
  40.  *
  41.  *   Internal Structures
  42.  *
  43.  ***********************************************************************/
  44. typedef struct 
  45.     {
  46.     UInt8 replaceme;
  47.     } StarterPreferenceType;
  48.  
  49. typedef struct 
  50.     {
  51.     UInt8 replaceme;
  52.     } StarterAppInfoType;
  53.  
  54. typedef StarterAppInfoType* StarterAppInfoPtr;
  55.  
  56.  
  57. /***********************************************************************
  58.  *
  59.  *   Global variables
  60.  *
  61.  ***********************************************************************/
  62. //static Boolean HideSecretRecords;
  63.  
  64.  
  65. /***********************************************************************
  66.  *
  67.  *   Internal Constants
  68.  *
  69.  ***********************************************************************/
  70. #define appFileCreator                 'STRT'
  71. #define appVersionNum              0x01
  72. #define appPrefID                  0x00
  73. #define appPrefVersionNum          0x01
  74.  
  75. // Define the minimum OS version we support (2.0 for now).
  76. #define ourMinVersion    sysMakeROMVersion(2,0,0,sysROMStageRelease,0)
  77.  
  78.  
  79. /***********************************************************************
  80.  *
  81.  *   Internal Functions
  82.  *
  83.  ***********************************************************************/
  84.  
  85.  
  86. /***********************************************************************
  87.  *
  88.  * FUNCTION:    RomVersionCompatible
  89.  *
  90.  * DESCRIPTION: This routine checks that a ROM version is meet your
  91.  *              minimum requirement.
  92.  *
  93.  * PARAMETERS:  requiredVersion - minimum rom version required
  94.  *                                (see sysFtrNumROMVersion in SystemMgr.h 
  95.  *                                for format)
  96.  *              launchFlags     - flags that indicate if the application 
  97.  *                                UI is initialized.
  98.  *
  99.  * RETURNED:    error code or zero if rom is compatible
  100.  *
  101.  * REVISION HISTORY:
  102.  *
  103.  *
  104.  ***********************************************************************/
  105. static Err RomVersionCompatible(UInt32 requiredVersion, UInt16 launchFlags)
  106. {
  107.     UInt32 romVersion;
  108.  
  109.     // See if we're on in minimum required version of the ROM or later.
  110.     FtrGet(sysFtrCreator, sysFtrNumROMVersion, &romVersion);
  111.     if (romVersion < requiredVersion)
  112.         {
  113.         if ((launchFlags & (sysAppLaunchFlagNewGlobals | sysAppLaunchFlagUIApp)) ==
  114.             (sysAppLaunchFlagNewGlobals | sysAppLaunchFlagUIApp))
  115.             {
  116.             FrmAlert (RomIncompatibleAlert);
  117.         
  118.             // Palm OS 1.0 will continuously relaunch this app unless we switch to 
  119.             // another safe one.
  120.             if (romVersion < ourMinVersion)
  121.                 {
  122.                 AppLaunchWithCommand(sysFileCDefaultApp, sysAppLaunchCmdNormalLaunch, NULL);
  123.                 }
  124.             }
  125.         
  126.         return sysErrRomIncompatible;
  127.         }
  128.  
  129.     return errNone;
  130. }
  131.  
  132.  
  133. /***********************************************************************
  134.  *
  135.  * FUNCTION:    GetObjectPtr
  136.  *
  137.  * DESCRIPTION: This routine returns a pointer to an object in the current
  138.  *              form.
  139.  *
  140.  * PARAMETERS:  formId - id of the form to display
  141.  *
  142.  * RETURNED:    void *
  143.  *
  144.  * REVISION HISTORY:
  145.  *
  146.  *
  147.  ***********************************************************************/
  148. static void * GetObjectPtr(UInt16 objectID)
  149. {
  150.     FormPtr frmP;
  151.  
  152.     frmP = FrmGetActiveForm();
  153.     return FrmGetObjectPtr(frmP, FrmGetObjectIndex(frmP, objectID));
  154. }
  155.  
  156.  
  157. /***********************************************************************
  158.  *
  159.  * FUNCTION:    MainFormInit
  160.  *
  161.  * DESCRIPTION: This routine initializes the MainForm form.
  162.  *
  163.  * PARAMETERS:  frm - pointer to the MainForm form.
  164.  *
  165.  * RETURNED:    nothing
  166.  *
  167.  * REVISION HISTORY:
  168.  *
  169.  *
  170.  ***********************************************************************/
  171. static void MainFormInit(FormPtr /*frmP*/)
  172. {
  173. }
  174.  
  175.  
  176.  
  177. static void MakeDocWindow()
  178. {
  179. }
  180.  
  181.  
  182. /***********************************************************************
  183.  *
  184.  * FUNCTION:    MainFormDoCommand
  185.  *
  186.  * DESCRIPTION: This routine performs the menu command specified.
  187.  *
  188.  * PARAMETERS:  command  - menu item id
  189.  *
  190.  * RETURNED:    nothing
  191.  *
  192.  * REVISION HISTORY:
  193.  *
  194.  *
  195.  ***********************************************************************/
  196. static Boolean MainFormDoCommand(UInt16 command)
  197. {
  198.     Boolean handled = false;
  199.    FormPtr frmP;
  200.  
  201.     switch (command)
  202.         {
  203.         case MainOptionsAboutDock:
  204.             MenuEraseStatus(0);                    // Clear the menu status from the display.
  205.             frmP = FrmInitForm (AboutForm);
  206.             FrmDoDialog (frmP);                    // Display the About Box.
  207.             FrmDeleteForm (frmP);
  208.             handled = true;
  209.             break;
  210.  
  211.         case MainOptionsRestoreSystemLauncher:
  212.             MenuEraseStatus(0);                    // Clear the menu status from the display.
  213.             PrefSetPreference(prefLauncherAppCreator,sysFileCLauncher);
  214.             handled = true;
  215.             break;
  216.  
  217.         }
  218.     
  219.     return handled;
  220. }
  221.  
  222. template <class T> T * HandleLock(MemHandle h)
  223. {
  224.     MemPtr p = MemHandleLock(h);
  225.     return reinterpret_cast<T*>(p);
  226. }
  227.  
  228. namespace {
  229.  
  230. MemHandle Duplicate(MemHandle h)
  231. {
  232.     UInt32 theSize(MemHandleSize(h));
  233.     MemHandle newH(MemHandleNew(theSize));
  234.     MemPtr pNew = MemHandleLock(newH);
  235.     MemPtr pOld = MemHandleLock(h);
  236.     MemMove(pNew, pOld, theSize);
  237.     MemHandleUnlock(newH);
  238.     MemHandleUnlock(h);
  239.     return newH;
  240. }
  241.  
  242. }
  243. static void LoadIcons()
  244. {
  245.     Boolean newSearch = true;
  246.     DmSearchStateType p = {};
  247.     LocalID localID(0);
  248.     Coord x(0), y(140);
  249.     UInt16 cardNoP;
  250.     while (DmGetNextDatabaseByTypeCreator(newSearch, &p, sysFileTApplication, 0, false, &cardNoP, &localID) == errNone) {
  251.         newSearch = false;
  252.         char dbName[dmDBNameLength];
  253.         UInt32 creator;
  254.         DmDatabaseInfo(0, localID, dbName, 0, 0, 0, 0, 0, 0, 0, 0, 0, &creator);
  255.         gNames.push_back(dbName);
  256.         gCreators.push_back(creator);
  257.         DmOpenRef theDB = DmOpenDatabase(0, localID, dmModeReadOnly);
  258.         MemHandle h(DmGetResource('tAIB', 1001));
  259.         if (h) {
  260.             gSmallIcons.push_back(Duplicate(h));
  261.             DmReleaseResource(h);
  262.         }
  263.         h = DmGetResource('tAIB', 1000);
  264.         if (h) {
  265.             gLargeIcons.push_back(Duplicate(h));
  266.             DmReleaseResource(h);
  267.         }
  268.         DmCloseDatabase(theDB);
  269.     }
  270. }
  271.  
  272. static void DrawCentered(const string& s, Coord x, Coord y, Int16 width)
  273. {
  274.     int textWidth = FntCharsWidth(s.data(), s.length());
  275.     x += (width/2);
  276.     x -= (textWidth/2);
  277.     WinDrawChars(s.data(), s.length(), x, y);
  278. }
  279.  
  280. static void DrawIcons()
  281. {
  282.     gIconLefts.clear();
  283.     Coord x(0), y(159);
  284.     RectangleType r = {0, 0, 160, 160};
  285.     WinEraseRectangle(&r, 0);
  286.     for(int i = 0; i < gSmallIcons.size(); ++i) {
  287.         MemHandle h = gSmallIcons[i];
  288.         Coord height = 9;
  289.         if (i == gTappedIcon) {
  290.             h = gLargeIcons[i];
  291.             height = 22;
  292.         }
  293.         BitmapType* bitMap = HandleLock<BitmapType>(h);
  294.         WinDrawBitmap(bitMap, x, y - height);
  295.         gIconLefts.push_back(x);
  296.         if (i == gTappedIcon) {
  297.             DrawCentered(gNames[i], x, y - height - FntLineHeight() - 2, bitMap->width);
  298.         }
  299.         x += bitMap->width;
  300.         x += 2;
  301.         MemHandleUnlock(h);
  302.     }
  303. }
  304.  
  305. static Boolean DoPenDownEvent(EventPtr eventP)
  306. {
  307.     if (eventP->screenY < 135)
  308.         return false;
  309.         
  310.     for (int i = 0; i < gIconLefts.size()-1; ++i) {
  311.         if (gIconLefts[i] < eventP->screenX && gIconLefts[i+1] > eventP->screenX)
  312.             gTappedIcon = i;
  313.     }
  314.     DrawIcons();
  315.     gDragging = true;
  316.     return true;
  317. }
  318.  
  319. static Boolean DoPenMovedEvent(EventPtr eventP)
  320. {
  321.     if (!gDragging)
  322.         return false;
  323.         
  324.     if (eventP->screenY < 135) {
  325.         gTappedIcon = -1;
  326.     } else {        
  327.         for (int i = 0; i < gIconLefts.size()-1; ++i) {
  328.             if (gIconLefts[i] < eventP->screenX && gIconLefts[i+1] > eventP->screenX)
  329.                 gTappedIcon = i;
  330.         }
  331.     }
  332.     DrawIcons();
  333.     return true;
  334. }
  335.  
  336. static void LaunchTappedIcon()
  337. {
  338.     AppLaunchWithCommand(gCreators[gTappedIcon], sysAppLaunchCmdNormalLaunch, NULL);
  339. }
  340.  
  341. static Boolean DoPenUpEvent(EventPtr eventP)
  342. {
  343.     if (!gDragging)
  344.         return false;
  345.         
  346.     if (eventP->screenY < 135) {
  347.         gTappedIcon = -1;
  348.     } else {        
  349.         for (int i = 0; i < gIconLefts.size()-1; ++i) {
  350.             if (gIconLefts[i] < eventP->screenX && gIconLefts[i+1] > eventP->screenX)
  351.                 gTappedIcon = i;
  352.         }
  353.     }
  354.     if (gTappedIcon != -1) {
  355.         LaunchTappedIcon();
  356.     }
  357.     gTappedIcon = -1;
  358.     gDragging = false;
  359.     DrawIcons();
  360.     return true;
  361. }
  362.  
  363. /***********************************************************************
  364.  *
  365.  * FUNCTION:    MainFormHandleEvent
  366.  *
  367.  * DESCRIPTION: This routine is the event handler for the 
  368.  *              "MainForm" of this application.
  369.  *
  370.  * PARAMETERS:  eventP  - a pointer to an EventType structure
  371.  *
  372.  * RETURNED:    true if the event has handle and should not be passed
  373.  *              to a higher level handler.
  374.  *
  375.  * REVISION HISTORY:
  376.  *
  377.  *
  378.  ***********************************************************************/
  379. static Boolean MainFormHandleEvent(EventPtr eventP)
  380. {
  381.    Boolean handled = false;
  382.    FormPtr frmP;
  383.  
  384.     switch (eventP->eType) 
  385.         {
  386.         case menuEvent:
  387.             return MainFormDoCommand(eventP->data.menu.itemID);
  388.         
  389.         case penDownEvent:
  390.             return DoPenDownEvent(eventP);
  391.             
  392.         case penMoveEvent:
  393.             return DoPenMovedEvent(eventP);
  394.             
  395.         case penUpEvent:
  396.             return DoPenUpEvent(eventP);
  397.             
  398.         case frmOpenEvent:
  399.             frmP = FrmGetActiveForm();
  400.             MainFormInit( frmP);
  401.             FrmDrawForm ( frmP);
  402.             DrawIcons();
  403.             handled = true;
  404.             break;
  405.  
  406.         default:
  407.             break;
  408.         
  409.         }
  410.     
  411.     return handled;
  412. }
  413.  
  414.  
  415. /***********************************************************************
  416.  *
  417.  * FUNCTION:    AppHandleEvent
  418.  *
  419.  * DESCRIPTION: This routine loads form resources and set the event
  420.  *              handler for the form loaded.
  421.  *
  422.  * PARAMETERS:  event  - a pointer to an EventType structure
  423.  *
  424.  * RETURNED:    true if the event has handle and should not be passed
  425.  *              to a higher level handler.
  426.  *
  427.  * REVISION HISTORY:
  428.  *
  429.  *
  430.  ***********************************************************************/
  431. static Boolean AppHandleEvent(EventPtr eventP)
  432. {
  433.     UInt16 formId;
  434.     FormPtr frmP;
  435.  
  436.     if (eventP->eType == frmLoadEvent)
  437.         {
  438.         // Load the form resource.
  439.         formId = eventP->data.frmLoad.formID;
  440.         frmP = FrmInitForm(formId);
  441.         FrmSetActiveForm(frmP);
  442.  
  443.         // Set the event handler for the form.  The handler of the currently
  444.         // active form is called by FrmHandleEvent each time is receives an
  445.         // event.
  446.         switch (formId)
  447.             {
  448.             case MainForm:
  449.                 FrmSetEventHandler(frmP, MainFormHandleEvent);
  450.                 break;
  451.  
  452.             default:
  453. //                ErrFatalDisplay("Invalid Form Load Event");
  454.                 break;
  455.  
  456.             }
  457.         return true;
  458.         }
  459.     
  460.     return false;
  461. }
  462.  
  463.  
  464. /***********************************************************************
  465.  *
  466.  * FUNCTION:    AppEventLoop
  467.  *
  468.  * DESCRIPTION: This routine is the event loop for the application.  
  469.  *
  470.  * PARAMETERS:  nothing
  471.  *
  472.  * RETURNED:    nothing
  473.  *
  474.  * REVISION HISTORY:
  475.  *
  476.  *
  477.  ***********************************************************************/
  478. static void AppEventLoop(void)
  479. {
  480.     UInt16 error;
  481.     EventType event;
  482.  
  483.     do {
  484.         EvtGetEvent(&event, evtWaitForever);
  485.  
  486.         if (! SysHandleEvent(&event))
  487.             if (! MenuHandleEvent(0, &event, &error))
  488.                 if (! AppHandleEvent(&event))
  489.                     FrmDispatchEvent(&event);
  490.  
  491.     } while (event.eType != appStopEvent);
  492. }
  493.  
  494.  
  495. /***********************************************************************
  496.  *
  497.  * FUNCTION:     AppStart
  498.  *
  499.  * DESCRIPTION:  Get the current application's preferences.
  500.  *
  501.  * PARAMETERS:   nothing
  502.  *
  503.  * RETURNED:     Err value 0 if nothing went wrong
  504.  *
  505.  * REVISION HISTORY:
  506.  *
  507.  *
  508.  ***********************************************************************/
  509. static Err AppStart(void)
  510. {
  511.     StarterPreferenceType prefs;
  512.     UInt16 prefsSize;
  513.  
  514.     // Read the saved preferences / saved-state information.
  515.     prefsSize = sizeof(StarterPreferenceType);
  516.     if (PrefGetAppPreferences(appFileCreator, appPrefID, &prefs, &prefsSize, true) != 
  517.         noPreferenceFound)
  518.         {
  519.         }
  520.     LoadIcons();
  521.     PrefSetPreference(prefLauncherAppCreator,'Dock');
  522.    return errNone;
  523. }
  524.  
  525. inline void MemHandleFreeOp(MemHandle h)
  526. {
  527.     MemHandleFree(h);
  528. }
  529.  
  530. static void FreeHandles(vector<MemHandle>& v)
  531. {
  532.     std::for_each(v.begin(), v.end(), MemHandleFreeOp);
  533. }
  534.  
  535. /***********************************************************************
  536.  *
  537.  * FUNCTION:    AppStop
  538.  *
  539.  * DESCRIPTION: Save the current state of the application.
  540.  *
  541.  * PARAMETERS:  nothing
  542.  *
  543.  * RETURNED:    nothing
  544.  *
  545.  * REVISION HISTORY:
  546.  *
  547.  *
  548.  ***********************************************************************/
  549. static void AppStop(void)
  550. {
  551.    StarterPreferenceType prefs;
  552.  
  553.     // Write the saved preferences / saved-state information.  This data 
  554.     // will be backed up during a HotSync.
  555.     PrefSetAppPreferences (appFileCreator, appPrefID, appPrefVersionNum, 
  556.         &prefs, sizeof (prefs), true);
  557.         
  558.     // Close all the open forms.
  559.     FrmCloseAllForms ();
  560.     FreeHandles(gSmallIcons);
  561.     FreeHandles(gLargeIcons);
  562. }
  563.  
  564.  
  565. /***********************************************************************
  566.  *
  567.  * FUNCTION:    StarterPalmMain
  568.  *
  569.  * DESCRIPTION: This is the main entry point for the application.
  570.  *
  571.  * PARAMETERS:  cmd - word value specifying the launch code. 
  572.  *              cmdPB - pointer to a structure that is associated with the launch code. 
  573.  *              launchFlags -  word value providing extra information about the launch.
  574.  *
  575.  * RETURNED:    Result of launch
  576.  *
  577.  * REVISION HISTORY:
  578.  *
  579.  *
  580.  ***********************************************************************/
  581. static UInt32 StarterPalmMain(UInt16 cmd, MemPtr /*cmdPBP*/, UInt16 launchFlags)
  582. {
  583.     Err error;
  584.  
  585.     error = RomVersionCompatible (ourMinVersion, launchFlags);
  586.     if (error) return (error);
  587.  
  588.     switch (cmd)
  589.         {
  590.         case sysAppLaunchCmdNormalLaunch:
  591.             error = AppStart();
  592.             if (error) 
  593.                 return error;
  594.                 
  595.             FrmGotoForm(MainForm);
  596.             AppEventLoop();
  597.             AppStop();
  598.             break;
  599.  
  600.         default:
  601.             break;
  602.  
  603.         }
  604.     
  605.     return errNone;
  606. }
  607.  
  608.  
  609. /***********************************************************************
  610.  *
  611.  * FUNCTION:    PilotMain
  612.  *
  613.  * DESCRIPTION: This is the main entry point for the application.
  614.  *
  615.  * PARAMETERS:  cmd - word value specifying the launch code. 
  616.  *              cmdPB - pointer to a structure that is associated with the launch code. 
  617.  *              launchFlags -  word value providing extra information about the launch.
  618.  * RETURNED:    Result of launch
  619.  *
  620.  * REVISION HISTORY:
  621.  *
  622.  *
  623.  ***********************************************************************/
  624. UInt32 PilotMain( UInt16 cmd, MemPtr cmdPBP, UInt16 launchFlags)
  625. {
  626.     return StarterPalmMain(cmd, cmdPBP, launchFlags);
  627. }
  628.